const Audio=require('wy-Audio')

cc.Class({
    extends: require('BaseUnit'),

    properties: {
        spanControl : require('SpanControl'),
        bloodLb :cc.Label,
        bodys : [cc.Node]
    }, 
    onLoad(){
        this.pathReady = this.node.getComponent('PathReady');
        this.pathReady&&this.pathReady.setAutoUpdate(false);
        this.launcher = this.node.getChildByName('launcher').getComponent('SkillLauncher')
        this.launcher.setSkillParentNode(global.GameLoop.GameMgr.no_BulletMonsterLayer)
        this.hitColor = new cc.Color(255,120,120);
        this.n_hitTime = -1;
        this.poolInit();
        this.canPass =false;
        if(this.camp==1){
            this.canPass=true;
        }
        global.canHit=true;
    },
    poolInit(){
        this.canDestroy=false;//是否能自动销毁
        this.n_bossTime = 0;
        this.isIgnore = true;
        this.b_isDie = false;
        this.attack = 20;
        this.grow = 1;
        this.launcher.poolInit();
    },
    setOptions(option,mgr){
        this.additional = option.additional;
        this.randomAdditionTime();

        this.prefab=option.prefab;
        this.b=option.cfg.keepTime;
        this.logicOptions = option.cfg;
        this.logicIndex = 0;
        this.monsterMgr = mgr;
        this.currentLogic =global.Common.deepCopy(this.logicOptions.status[this.logicIndex]);
        this.blood = this.logicOptions.blood * option.grow//血量
        this.launcher.grow = option.grow
        this.score = this.logicOptions.score; //击败后掉落的分数
        this.isIgnore = true;                 // 是否是无敌状态
        this.n_ignoreTime = 0;
        this.keepTime = option.keepTime;
        // this.keepTime = this.logicOptions.keepTime;
        this.option = option;
        this.testblood = [];
    },
    randomAdditionTime() {
        // this.n_bossTime = this.additional.time + Common.randomInteger();
    },
    runLogic(logic){
        if(this.isIgnore != logic.isIgnore){
            this.isIgnore = logic.isIgnore;
            this.node.emit('monster-ignore',this)
        }

        if(logic.path){
            var path = global.PathConfig[logic.path];
            path.name = logic.path
            logic.pathFlip &&(this.pathReady.setFlip(logic.pathFlip.x,logic.pathFlip.y))
            this.pathReady.loadData(path);
            this.pathReady.runAction(1)
        }
        this.launcher.reset();
        if(logic.skill){
            var r = 0;
            while(logic.skill[r]){
                this.launcher.addLauncher(logic.skill[r])
                r++;
            }
        }
        if(logic.span && this.spanControl){
            this.spanControl.playDBArmature(logic.span)
        }
        if(logic.action){
            this.runCCAction(logic.action)
        }   
    },
    setIgnore(r,time){ //设置对象无敌
        this.isIgnore = r;
        this.node.emit('monster-ignore',this)
        this.n_ignoreTime = time;
    },

    runCCAction(data){
        var arr = data.split(',')
        var type = arr[0];
        var action = null;
        if(type == 'r'){
            this.node.getChildByName('body').rotation = arr[2]
            action = cc.rotateTo(arr[1],arr[3]);
            this.node.getChildByName('body').runAction(action)
        } else if(type == 'mb'){
            action = cc.moveBy(arr[1],cc.v2(arr[2],arr[3]));
            this.node.runAction(action)
        } else if(type == 'mt'){
            action = cc.moveTo(arr[1],cc.v2(arr[2],arr[3]));
            this.node.runAction(action)
        }
    },
    playBombEffect(){
        var self = this;
        this.node.stopAllActions();
        this.node.runAction(cc.fadeOut(0.3))
        this.scheduleOnce(function(){
            self.node.destroy();
        },0.5)
    },
    staticsBlood(blood){
        var time = Date.now();
        this.testblood.push({
            time:time,
            blood : blood
        })

        if(this.testblood.length){
            var i=0;
            var ab = 0;
            while(i<this.testblood.length){
                if(time - this.testblood[i].time > 1000){
                    this.testblood.splice(0,1);
                } else {
                    ab+=this.testblood[i].blood;
                    i++;
                    // break;
                }
            }
            this.bloodLb && (this.bloodLb.string = ab);
        }
    },
    onAttack(bullet){ //受到攻击
        // cc.log('=======',bullet.node.x)
        if(!bullet.attack){
            cc.warn('子弹没有伤害?????????????')
        }
        if(this.isIgnore){ //无敌状态 
            return;
        }
        this.blood -= bullet.getAttack()
        if(this.n_hitTime == -1){
            for(var i=0; i<this.bodys.length; i++){
                this.bodys[i].color = this.hitColor;
            }
        }
        this.n_hitTime = 0.1;
        // cc.log(this.node.name,',血量剩下:',this.blood)
        this.staticsBlood(bullet.getAttack())
        

        if(bullet.skillbase.bulletType=='bomb'){
            if(this.prefab.slice(0,4)=='boss' || this.prefab.slice(0,9)=='smallBoss'){
                
            }else{
                this.monsterMgr.destroyMonster(this);
                this.onDisable();
                this.b_isDie = true;
                this.launcher.reset();
                this.playBombEffect()
                
                global.GameLoop.GameMgr.particleMgr.createCoin(this.score,this);
            }
        } else {
            global.GameLoop.GameMgr.sound.playHit();
        }


        if(this.blood <=0 ){
            
            if (CC_WECHATGAME && wx['vibrateShort'] && (this.prefab.split('_')[1]=='3' || this.prefab.split('_')[1]=='4')) {
                wx.vibrateShort()//wx.vibrateLong()
            }

            if((this.prefab.slice(0,4)=='boss' || this.prefab.slice(0,9)=='smallBoss') && global.gates!='0'){
                global.canHit=false;
            }
            global.enemyCount++;
            this.monsterMgr.destroyMonster(this);
            this.onDisable();
            this.b_isDie = true;
            this.launcher.reset();
            this.playBombEffect()
            
            global.GameLoop.GameMgr.particleMgr.createCoin(this.score,this);
            global.GameLoop.GameMgr.sound.playBomb();
        } else if(bullet.hit){
            var hit = global.NodePool.get('bulletHit',this.node,{})
            hit.scaleX = hit.scaleY = 0.1
            hit.x=bullet.node.x-this.node.x; 
            hit.y=-this.node.height/2;
            hit.js.setOptions(bullet.hit);
        }
    },
    onColliderEnter(collider){ //如果飞机接收到了碰撞事件 一定是飞机间的
        collider.onAttack(this);
    },
    onDestroy(){
        this.node.emit('child-removed',this.node);
    },
    gameUpdate(dt){
        
        this.n_bossTime -= dt;
        if (this.n_bossTime < 0 && this.additional!=undefined && this.additional!=null) {
            this.n_bossTime = this.additional.time
            var len = Math.floor(this.additional.list.length * Math.random());
            this.monsterMgr.createEnemyTeam(this.additional.list[len])
        }

        if(this.n_ignoreTime && this.n_ignoreTime>0){
            this.n_ignoreTime -= dt;
            this.setIgnore(true,this.n_ignoreTime);
            if(this.n_ignoreTime <= 0){
                this.setIgnore(false);
            }
        }
        if(this.n_hitTime >= 0){
            this.n_hitTime -= dt;
            if(this.n_hitTime< 0){
                this.n_hitTime = -1;
                for(var i=0; i<this.bodys.length; i++){
                    this.bodys[i].color = cc.Color.WHITE;
                }
            }
        }
        if(this.b_isDie){
            return;
        }
        if(this.launcher){
            this.launcher.gameUpdate(dt);
        }
        if(!this.currentLogic){
            return;
        }
        if(!this.currentLogic.delay){
            this.currentLogic.delay = -1
            this.runLogic(this.currentLogic);
            return
        } else if(this.currentLogic.delay>=0){
            this.currentLogic.delay -= dt;
            if(this.currentLogic.delay < 0){
                this.runLogic(this.currentLogic);
            }
            return;
        }
        if(this.currentLogic.time >= 0){
            this.currentLogic.time -= dt;
            if(this.currentLogic.time < 0){
                if(typeof(this.currentLogic.to) != 'undefined'){
                    this.logicIndex = this.currentLogic.to
                } else {
                    this.logicIndex++;
                }
                this.currentLogic = global.Common.deepCopy(this.logicOptions.status[this.logicIndex])
                if(!this.currentLogic){
                    // cc.log('逻辑完成')
                    this.node.destroy();
                }
            }
        }
    }
});
